#include "../main.h"

#ifdef __BUILD_SKEL

#include <stdio.h>
#include <stdlib.h>

#include "skeleton.h"
#include "../utils.h"
#include "../xmalloc.h"

__BLTIN_ST char *skel_var_args[] = { SKELETON_ARG0, SKELETON_ARG1, NULL };
__BLTIN_VAR skel_var = { SKELETON_VAR_NAME, skel_var_args, { __skel_preinit, __skel_shutdown, __skel_init, __skel_free, __skel_foundvar, __skel }, VAR_EXTENDED | VAR_TMPLT_REQ };

__BLTIN_RT void *skel_dbhandle;


int
__skel_preinit(void)
{
  if (url_register(SKELETON_URI, SKELETON_PATH, URL_BUILTIN, NULL) == NULL) {
    return (1);
  } else {
    return (0);
  }
}

int
__skel_shutdown(void)
{
  return (0);
}

int
__skel_init(void)
{
  int ret;
  ret = db_init(SKELETON_DB_PREFIX, &skel_dbhandle);
  return (ret);
}

int
__skel_free(void)
{
  db_close(skel_dbhandle);
  return (0);
}

void
__skel_foundvar(const struct tag_data *td __attribute__((__unused__)) )
{
}

void
__skel(const struct var_reqdata *vr)
{
  const tmpl_t *tmpl;
  tmpl_data_t *tmpl_data;
  string_t *tmp;
  const char *testvar;
  char *arg0, *arg1;

  hashmap_get(vr->qry, SKELETON_HTTP_GET_VAR, (any_t *) &testvar);
  TMPL_SIMPLE(tmpl, tmpl_data, vr, SKELETON_TMPL_DEF);

  GET_ATTR(vr->attrs, SKELETON_ARG0, SKELETON_ARG0_DEFAULT, arg0);
  GET_ATTR(vr->attrs, SKELETON_ARG1, SKELETON_ARG1_DEFAULT, arg1);

  tmp = create_empty_string();
  set_string(tmp, "[SKELETON]");
  add_sstring(vr->result, tmp);
  free_string(tmp);
}

#endif
